package cn.wolfcode.luowowo.community.service.impl;

import cn.wolfcode.luowowo.community.domain.CommunityAnswer;
import cn.wolfcode.luowowo.community.domain.CommunityQuestion;
import cn.wolfcode.luowowo.community.repository.CommunityAnswerRepository;
import cn.wolfcode.luowowo.community.service.ICommunityAnswerService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CommunityAnswerServiceImpl implements ICommunityAnswerService {

    @Autowired
    private CommunityAnswerRepository repository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference
    private ICommunityQuestionService communityQuestionService;

    public CommunityAnswer queryGoldReplyByQuestionId(Long questionId) {
        // 查询所有questionId为传入questionId的文档, 并按照顶的数量倒叙排序, 取第一条
        Query query = new Query();
        query.addCriteria(Criteria.where("questionId").is(questionId));
        query.addCriteria(Criteria.where("type").is(CommunityAnswer.TYPE_ANSWER));
        query.with(Sort.by(Sort.Order.desc("thumbnum")));
        List<CommunityAnswer> list = mongoTemplate.find(query, CommunityAnswer.class, "community_answer");
        CommunityAnswer answer = null;
        if (list != null && list.size() > 0) {
            answer = list.get(0);

            // 查询金牌回答下的所有回复,先不做
        } else {
            answer = null;
        }
        return answer;
    }

    public void save(CommunityAnswer communityAnswer) {
        repository.save(communityAnswer);
    }

    public int QueryForCount(Long questionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("questionId").is(questionId));
        List<CommunityAnswer> list = mongoTemplate.find(query, CommunityAnswer.class, "community_answer");
        return list != null ? list.size() : 0;
    }

    public List<CommunityAnswer> QueryAllButGoldReply(Long questionId) {
        // 先查出所有, 按发布时间排序
        Query query = new Query();
        query.addCriteria(Criteria.where("questionId").is(questionId));
        query.with(Sort.by(Sort.Order.desc("releaseTime")));
        List<CommunityAnswer> list = mongoTemplate.find(query, CommunityAnswer.class, "community_answer");

        // 删除金牌回答
        CommunityAnswer goldReply = this.queryGoldReplyByQuestionId(questionId);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(goldReply.getId())) {
                list.remove(i);
            }
        }
        return list;
    }

    public void addReply(Long questionId, UserInfo userInfo, String content) {
        CommunityAnswer answer = new CommunityAnswer();

        // 先查出当前问题相关信息
        CommunityQuestion question = communityQuestionService.get(questionId);

        // 设置相关字段
        answer.setQuestionId(questionId);
        answer.setContent(content);
        answer.setDestId(question.getDest().getId());
        answer.setDestName(question.getDest().getName());
        answer.setUserId(userInfo.getId());
        answer.setUsername(userInfo.getNickname());
        answer.setLevel(userInfo.getLevel());
        answer.setCity(userInfo.getCity());
        answer.setHeadImgUrl(userInfo.getHeadImgUrl());
        answer.setReleaseTime(new Date());
        repository.save(answer);
    }

    public void addThumb(String answerId, int count) {
        Optional<CommunityAnswer> answerOptional = repository.findById(answerId);
        CommunityAnswer answer = answerOptional.get();
        answer.setThumbnum(answer.getThumbnum() + count);
        repository.save(answer);
    }
}
